package cn.zwx.security.example.common;

import cn.zwx.security.example.common.enums.ResultCodeEnum;
import cn.zwx.security.example.common.exception.BizException;
import lombok.Data;

/**
 * @description:
 * @projectName:spring-boot-security-example
 * @see:cn.zwx.security.example.common
 * @author:zhangwenxue
 * @createTime:2021/6/23 10:26
 * @version:1.0
 */
@Data
public class ResponseEntry<T> {

    //是否成功
    private Boolean success;
    //状态码
    private Integer code;
    //提示信息
    private String msg;
    //数据
    private T data;
    public ResponseEntry() {

    }
    //自定义返回结果的构造方法
    public ResponseEntry(Boolean success,Integer code, String msg,T data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    // 返回成功
    public static ResponseEntry ok(Object data){
        ResponseEntry result = new ResponseEntry();
        result.setSuccess(Boolean.TRUE);
        result.setCode(ResultCodeEnum.SUCCESS.getErrorCode());
        result.setMsg(ResultCodeEnum.SUCCESS.getErrorMsg());
        result.setData(data);
        return result;
    }

    public static ResponseEntry ok(){
        ResponseEntry result = new ResponseEntry();
        result.setSuccess(Boolean.TRUE);
        result.setCode(ResultCodeEnum.SUCCESS.getErrorCode());
        result.setMsg(ResultCodeEnum.SUCCESS.getErrorMsg());
        return result;
    }

    // 返回失败
    public ResponseEntry failed(int code,String msg){
        ResponseEntry result = new ResponseEntry();
        result.setSuccess(Boolean.FALSE);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }


    //自定义异常返回的结果
    public static ResponseEntry defineError(BizException de){
        ResponseEntry result = new ResponseEntry();
        result.setSuccess(Boolean.FALSE);
        result.setCode(de.getErrorCode());
        result.setMsg(de.getErrorMsg());
        return result;
    }

    //其他异常处理方法返回的结果
    public static ResponseEntry otherError(ResultCodeEnum errorEnum){
        ResponseEntry result = new ResponseEntry();
        result.setMsg(errorEnum.getErrorMsg());
        result.setCode(errorEnum.getErrorCode());
        result.setSuccess(Boolean.FALSE);
        return result;
    }
    //其他异常处理方法返回的结果
    public static ResponseEntry otherError(int errorEnum,String msg){
        ResponseEntry result = new ResponseEntry();
        result.setMsg(msg);
        result.setCode(errorEnum);
        result.setSuccess(Boolean.FALSE);
        return result;
    }

}
